﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.TipsCommands
{
    public class BreedTipsSearchHandler : IRequestHandler<BreedTipsSearchCommand, Result>
    {
        private readonly IBaseRepository<SleepInfor> sleepinforbaseRepository;
        private readonly IBaseRepository<Cottages> cottagebaseRepository;
        private readonly IBaseRepository<Fields> fieldsRepository;

        public BreedTipsSearchHandler(IBaseRepository<SleepInfor> sleepinforbaseRepository, IBaseRepository<Fields> fieldsRepository, IBaseRepository<Cottages> cottagebaseRepository)
        {
            this.sleepinforbaseRepository = sleepinforbaseRepository;
            this.fieldsRepository = fieldsRepository;
            this.cottagebaseRepository = cottagebaseRepository;
        }
        /// <summary>
        /// 业务逻辑（配种提示）
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(BreedTipsSearchCommand request, CancellationToken cancellationToken)
        {
            var result= new Result() { Code=(int)CodeEnum.search};
            //var list= sleepinforbaseRepository.GetAll().Where(x=>x.Gender==2&&x.BreedStatuId==1||x.BreedStatuId==2 || x.BreedStatuId == 3 || x.BreedStatuId == 4 || x.BreedStatuId == 5).ToList();
            var list= (from a in sleepinforbaseRepository.GetAll()
                      join b in cottagebaseRepository.GetAll() on a.RecordcottageId equals b.CottagesId
                      join c in  fieldsRepository.GetAll() on a.FieIdId equals c.FieIdId
                      where a.Gender==2&&(a.BreedStatuId == 1 || a.BreedStatuId == 2 || a.BreedStatuId == 3 || a.BreedStatuId == 4 || a.BreedStatuId == 5 )
                      select new TipsDto()
                      {
                          Gender = a.Gender,
                          BreedStatuId = a.BreedStatuId,
                          EarNumber=a.EarNumber,
                          SleepEntryDate=a.SleepEntryDate,
                          CottageName=b.CottageName,
                          FieIdName=c.FieIdName,
                          agetotal=(int)(DateTime.Now-Convert.ToDateTime(a.SleepEntryDate)).TotalDays
                      }).ToList();
            result.Data = list;
            result.Count = list.Count;
            return result;
        }
    }
}
